package animator.phantom.controller;

/*
    Copyright Janne Liljeblad 2006,2007,2008

    This file is part of Phantom2D.

    Phantom2D is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Phantom2D is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Phantom2D.  If not, see <http://www.gnu.org/licenses/>.
*/

import java.util.*;

//--- Data of video and audio codes, file xtensions that are available for a video container format.
public class VideoFormat
{
	//--- Video formats
	public static final String AVI = "avi";
	public static final String MOV = "mov";
	public static final String MPG = "mpg";
	public static final String MP4 = "mp4";
	public static final String FLASH = "Flash Video";
	public static final String REAL = "RealVideo";
	public static final String WMV8 = "Windows Media Video 8";
	public static final String PALDVD = "PAL DVD MPEG-2";
	public static final String MATROSKA = "matroska";
	public static final String DV = "DV";
	public static final String OGG = "ogg";
	public static final String THREEGP = "3gp"; 

	protected static Hashtable<String, String> formatExtensions = new Hashtable<String, String>();
	private static Vector<String> formats = new Vector<String>();

	//--- default output file extensions.
	static
	{
		//--- Possible video formats
		formats.add( AVI );
		formats.add( MOV );
		formats.add( MPG );
		formats.add( MP4 );
		formats.add( FLASH );
		formats.add( REAL );
		formats.add( WMV8 );
		formats.add( PALDVD );
		formats.add( MATROSKA );
		formats.add( DV );
		formats.add( OGG );
		formats.add( THREEGP ); 

		//--- default format extensions.
		formatExtensions.put( AVI, "avi");
		formatExtensions.put( MOV, "mov");
		formatExtensions.put( MPG, "mpg");
		formatExtensions.put( MATROSKA, "mkv");
		formatExtensions.put( DV, "dv");
		formatExtensions.put( MP4, "mp4");
		formatExtensions.put( OGG, "ogg" );
	}

	private String format;
	private Vector<VideoCodec> videoCodecs = new Vector<VideoCodec>();
	private Vector<AudioCodec> audioCodecs = new Vector<AudioCodec>();

	public VideoFormat( String format )
	{
		this.format = format;
		//--- only formats in formats vec are accepted.
		boolean found = false;
		for( String testformat : formats )
			if( format.equals( testformat ) ) found = true;

		if( found == false )
		{
			
			System.out.println("Unrecognised video format " + format + " was created. See VideoFormat.java for acceptable videoformats. Aborting..." );
			System.exit( 1 );
		}
	}

	//--- Returns currently selected video codec.
	public VideoCodec vCodec()
	{
		for( int i = 0; i < videoCodecs.size(); i++ )
		{
			VideoCodec vc = videoCodecs.elementAt( i );
			if( vc.getCodec().equals( CodecController.cVCodec ) ) return vc;
		}
		return null;//something has gone wrong, codec that is not part of selected format is selected
	}

	public AudioCodec aCodec()
	{
		for( int i = 0; i < audioCodecs.size(); i++ )
		{
			AudioCodec ac = audioCodecs.elementAt( i );
			if( ac.getCodec().equals( CodecController.cACodec ) ) return ac;
		}
		return null;//something has gone wrong, codec that is not part of selected format is selected
	}

	//--- Returns names of videocodecs in this format.
	public String[] getVCodecs()
	{
		String[] retCodecs = new String[ videoCodecs.size() ];
		for( int i = 0; i < videoCodecs.size(); i++ )
			retCodecs[ i ] = videoCodecs.elementAt( i ).getCodec();

		return retCodecs;
	}

	//--- Returns names of videocodecs in this format.
	public String[] getACodecs()
	{
		String[] retCodecs = new String[ audioCodecs.size() ];
		for( int i = 0; i < audioCodecs.size(); i++ )
			retCodecs[ i ] = audioCodecs.elementAt( i ).getCodec();

		return retCodecs;
	}
	
	//--- Returns name of this format.
	public String getFormat(){ return format; }
	public String getExtension(){ return formatExtensions.get( format ); }
	public String getExtension( String format_ ){ return formatExtensions.get( format_ ); }

	public void addVCodec( VideoCodec vcod ){ videoCodecs.add( vcod ); }
	public void addACodec( AudioCodec acod ){ audioCodecs.add( acod ); }

}//end class